<?php

namespace app\controller;

use app\BaseController;
use app\dto\WenZhangDto;
use app\model\BaseQuery;
use app\model\ModifyQuery;
use app\model\User;
use app\model\WenZhang;
use app\Request;
use app\utils\Utils;
use app\validate\WenzhangValidate;

function toWenzhangDto(WenZhangDto $params)
{
    return $params;
}

class WenzhangController extends BaseController
{

    function pageList(Request $request)
    {
        $wenZhangDto = $request->all();
        $query = BaseQuery::build(WenZhang::class);
        if (!Utils::isNullPlus($wenZhangDto,'name')) {
            $query->where("name", "like", $wenZhangDto['name'] . '%');
        }
        if (!Utils::isNullPlus($wenZhangDto,'id')) {
            $query->where("id", $wenZhangDto['id']);
        }
        $data = $query->page($wenZhangDto['pageNum']??1)
            ->limit($wenZhangDto['pageSize']??10)
            ->select();
        return Utils::ok(
            $data,
            $query->count());
    }

    function  add(Request  $request){
        $params=$request->all();
        if(!Utils::isNullPlus($params,'id')){
            return Utils::failRes("新增不能有id");
        }
       /* if(Utils::isNullPlus($params,'createUserId')){
            return Utils::failRes("createUserId  不能为空");
        }*/
        /*校验 */
        validate(WenzhangValidate::class)->scene('add')->check($params);

        /*查询是否存在*/
        return Utils::ok(WenZhang::create($params));
    }
    function  update(Request  $request){
        $params=$request->all();
        /*校验 */
        if(Utils::isNullPlus($params,'id')){
            return Utils::failRes("id 必填");
        }
        validate(WenzhangValidate::class)->scene('update')->check($params);

      /*查询是否存在*/
        $query = BaseQuery::build(WenZhang::class);
        $one = $query->where("id", $params['id']) -> find();
        if(Utils::isNullPlus($one,'id')){
            return Utils::failRes("id:{$params['id']} 没有找到数据");
        }
        $modifyQuery = ModifyQuery::build(WenZhang::class);
        $resultint=$modifyQuery->update($params,[
            'id'=>$params['id']
        ]);

        return Utils::ok($resultint);
    }
    function  del(Request  $request){
        $params=$request->all();
        /*校验 */
        validate(WenzhangValidate::class)->scene('del')->check($params);

        /*查询是否存在*/
        $one = WenZhang::find($params['id']);
        if($one&&$one['id']){
            return Utils::failRes("id:{$params['id']} 没有找到数据");
        }
        $result = BaseQuery::build(WenZhang::class)->where("id", $params['id'])
            ->useSoftDelete('delete_time', Utils::dateyyyymmddhhmmss(time()))
            ->delete();
        return Utils::ok($result);
    }

}